from django.forms import ModelForm
class BootStrapModelForm(ModelForm):
    # 利用之前定义的基类中的`exclude_bootstrap=[]`来控制样式的添加
    # checkbox无需添加样式
    exclude_bootstrap = [
        'before_download_template',
        'after_download_template',
        'before_deploy_template',
        'after_deploy_template'

    ]

    def __init__(self, *args, **kwargs):
        # 执行父类的 __init__
        super().__init__(*args, **kwargs)

        # 自定义功能，为字段添加Bootstrap样式
        # self.fields就是代表总共有几个字段
        # self.fields['hostname'].widget.attrs['class'] = 'form-control'
        for k, field in self.fields.items():
            if k in self.exclude_bootstrap:
                continue
            field.widget.attrs['class'] = 'form-control'

